package protocol

import (
	"encoding/binary"
	"gitee.com/crack007/goose"
	"gitee.com/crack007/goose/core/server"
	"gitee.com/crack007/udp-penetrate/src/common/constant"
	"net"
)

// client和server的首次握手登记
type HandshakeRespProtocol struct {
	Host string // client的公网ip
	Port uint16 // client的公网端口
}

func NewHandshakeRespProtocol() *HandshakeRespProtocol {
	return &HandshakeRespProtocol{}
}
func (p *HandshakeRespProtocol) Code() uint32 {
	return constant.CMD_HANDSHANKE_RESP
}

// client端处理
func (p *HandshakeRespProtocol) Process(session *server.Session, clientConn *net.UDPConn) IProtocol {
	goose.GetLogger().Debug("Network info-> host=%s port=%d", p.Host, p.Port)
	return nil
}

func (p *HandshakeRespProtocol) Marshall() []byte {
	var body = make([]byte, 4)
	binary.BigEndian.PutUint16(body, p.Port)
	body = append(body, []byte(p.Host)...)
	return body
}

func (p *HandshakeRespProtocol) UnMarshall(data []byte) {
	p.Port = binary.BigEndian.Uint16(data[:4])
	p.Host = string(data[4:])
}
